// 路由
import Vue from "vue";
import VueRouter from "vue-router";
// 引入路由规则
import routes from "@/router/routes"
// 使用插件
Vue.use(VueRouter);
// 配置路由
const router = new VueRouter({
  // routes路由规则
  routes,
  // 设置路由跳转后滚轮在顶部
  scrollBehavior(to,from,savedPosition){
    return {y:0}
  }
})
// 先将原来的方法保存一份，后面还要用到这个方法，后面重新方法会把原来的覆盖了
const originPush = VueRouter.prototype.push;
const originReplace = VueRouter.prototype.replace;
// 重写push、replace方法，解决多次传同样的值会报错的问题
VueRouter.prototype.push = function(location,resolve,reject){
  // 原来的push方法在保存的时候this指向了window，要改变this的指向
  if(resolve&&reject){
      originPush.call(this,location,resolve,reject)
    }else if(resolve){
      originPush.call(this,location,resolve,()=>{})
    }else if(reject){
      originPush.call(this,location,()=>{},reject)
    }else {
      originPush.call(this,location,()=>{},()=>{})
    }
}
VueRouter.prototype.replace = function(location,resolve,reject){
  // 原来的push方法在保存的时候this指向了window，要改变this的指向
  if(resolve&&reject){
    originReplace.call(this,location,resolve,reject)
    }else if(resolve){
      originReplace.call(this,location,resolve,()=>{})
    }else if(reject){
      originReplace.call(this,location,()=>{},reject)
    }else {
      originReplace.call(this,location,()=>{},()=>{})
    }
}
// console.log(router)
// 向外暴露router
export default router